package com.nullpointer.xchat.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.object.HistoryMessage;
import com.nullpointer.xchat.object.MessageContent;
import com.nullpointer.xchat.object.MessageContent.MsgType;
import com.nullpointer.xchat.utils.Emoticons;

public class HistoryMessageAdapter extends ArrayAdapter<HistoryMessage> {

	/**
	 * ViewHolder class for layout.<br />
	 * <br />
	 * Auto-created on 2014-11-03 16:38:39 by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 */
	private static class ViewHolder {
		public final LinearLayout rootView;
		public final ImageView ivAvatar;
		public final TextView tvName;
		public final ImageView ivMark;
		public final TextView tvMessage;
		public final TextView tvMessageNum;

		private ViewHolder(LinearLayout rootView, ImageView ivAvatar,
				TextView tvName, ImageView ibMark, TextView tvMessage,
				TextView tvMessageNum) {
			this.rootView = rootView;
			this.ivAvatar = ivAvatar;
			this.tvName = tvName;
			this.ivMark = ibMark;
			this.tvMessage = tvMessage;
			this.tvMessageNum = tvMessageNum;
		}

		public static ViewHolder create(LinearLayout rootView) {
			ImageView ivAvatar = (ImageView) rootView
					.findViewById(R.id.iv_avatar);
			TextView tvName = (TextView) rootView.findViewById(R.id.tv_name);
			ImageView ibMark = (ImageView) rootView.findViewById(R.id.iv_mark);
			TextView tvMessage = (TextView) rootView
					.findViewById(R.id.tv_message);
			TextView tvMessageNum = (TextView) rootView
					.findViewById(R.id.tv_message_num);
			return new ViewHolder(rootView, ivAvatar, tvName, ibMark,
					tvMessage, tvMessageNum);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_history, parent,
					false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		HistoryMessage item = getItem(position);
		if (item.getUnreadNum() > 10) {
			vh.tvMessageNum.setText("9+");
			vh.tvMessageNum.setVisibility(View.VISIBLE);
		} else if (item.getUnreadNum() > 0) {
			vh.tvMessageNum.setText(String.valueOf(item.getUnreadNum()));
			vh.tvMessageNum.setVisibility(View.VISIBLE);
		} else {
			vh.tvMessageNum.setVisibility(View.GONE);
		}

		if (item.isFriend()) {
			vh.ivMark.setImageResource(R.drawable.ic_action_mark_friend);
			vh.ivMark.setVisibility(View.VISIBLE);
		} else {
			vh.ivMark.setVisibility(View.GONE);
		}

		AQuery aq = new AQuery(convertView);
		MessageContent messageContent = item.getMessageContent();
		vh.tvName.setText(messageContent.getName());
		if (messageContent.getMsgType() == MsgType.TEXT) {
			vh.tvMessage.setText(Emoticons.getSmiledText(context,
					messageContent.getContent()));
		} else {
			vh.tvMessage.setText("Send an image!");
		}
		if (messageContent.getAvartar() != null) {
			ImageOptions options = new ImageOptions();
			options.round = 5;
			options.fileCache = true;
			options.memCache = true;
			options.targetWidth = 64;
			// options.fallback =
			aq.id(vh.ivAvatar).image(messageContent.getAvartar(), options);
		} else {
			vh.ivAvatar.setImageResource(R.drawable.ic_no_avatar);
		}
		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private Context context;

	// Constructors
	public HistoryMessageAdapter(Context context, List<HistoryMessage> objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public HistoryMessageAdapter(Context context, HistoryMessage[] objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}
}
